package com.jmz.apt.api.message.response;

import com.jmz.apt.api.Utils.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanglexue on 2019/10/28.
 */
@Component
public class ResponseMessageTemplate {

    /**
     * 内部私有类
     */
    private ResponseMessage responseMessage;

    /**
     * 默认构造函数
     */
    public ResponseMessageTemplate() {
        this.responseMessage = new ResponseMessage();
        this.initResponse();
    }


    public  void  initResponseMessage(){
        this.initResponse();
        this.initDataSet();
        this.initQueryResponse();
        this.initBussResponse();
    }

    public ResponseMessage getResponseMessage() {
        return this.responseMessage;
    }


    /**
     * 设置消息响应状态和说明
     *
     * @param code 取值范围： {@link Response#RESPONSE_SUCCESS} {@link Response#RESPONSE_FAIL}
     * @param text 响应说明
     */
    public ResponseMessage setResponse(int code, String text) {
        this.responseMessage.getMessage().getBody().getResponse().setCode(code);
        this.responseMessage.getMessage().getBody().getResponse().setText(text);
        return this.responseMessage;
    }

    /**
     * 设置消息响应状态和说明
     *
     * @param code   取值范围： {@link Response#RESPONSE_SUCCESS} {@link Response#RESPONSE_FAIL}
     * @param text   响应说明
     * @param detail 详细信息
     */
    public ResponseMessage setResponse(int code, String text, String detail) {
        this.responseMessage.getMessage().getBody().getResponse().setCode(code);
        this.responseMessage.getMessage().getBody().getResponse().setText(text);
        this.responseMessage.getMessage().getBody().getResponse().setDetail(detail);
        return this.responseMessage;
    }

    /**
     * 初始化响应参数
     */
    public ResponseMessage initResponse() {
        this.responseMessage.getMessage().getBody().setResponse(null);
        return this.responseMessage;
    }


    /**
     * 获取Response信息
     *
     * @return 响应信息
     */
    public Response getResponse() {
        return this.responseMessage.getMessage().getBody().getResponse();
    }


    /**
     * 向响应体的DataSet中增加data子集
     *
     * @param name data子集的名称
     * @param row  data子集的数据行
     */
    public void addData(String name, List<Object> row) {
        Data data = new Data(name, row);
        if (this.getDataset()== null) {
            List<Data> datas = new ArrayList();
            datas.add(data);
            this.setDataset(datas);
        } else {
            this.getDataset().add(data);
        }
    }

    /**
     * 设置响应的数据集合
     *
     * @param datas 数据集
     */
    public void setDataset(List<Data> datas) {
        this.responseMessage.getMessage().getBody().setDataset(datas);
    }

    public void  initDataSet(){
        this.responseMessage.getMessage().getBody().setDataset(null);
    }

    /**
     * 获取消息体中的数据集合
     *
     * @return {@link Body#getDataset()}
     */
    public List<Data> getDataset() {
        return this.responseMessage.getMessage().getBody().getDataset();
    }

    /**
     * 根据报文中DataSet的名字得到row
     *
     * @param key dataset中的name节点
     * @return 返回row节点的内容
     */
    public List<Object> getDataSetByKey(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<Data> datas = this.responseMessage.getMessage().getBody().getDataset();
        for (Data data : datas) {
            if (StringUtils.isBlank(data.getName())) continue;

            if (key.equalsIgnoreCase(data.getName().trim())) {
                return data.getRow();
            }
        }
        return null;
    }


    /**
     * 根据报文中DataSet的名字得到row
     * @param key 记录集合的名称
     * @param clazz 需要将数据集合转换成的集合对象
     * @return
     */
    public List<Object> getDataSetByKey(String key, Class clazz) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        List<Data> datas = this.responseMessage.getMessage().getBody().getDataset();
        for (Data data : datas) {
            if (StringUtils.isBlank(data.getName())) continue;

            if (key.equalsIgnoreCase(data.getName().trim())) {
                return ObjectUtil.convertListMap2ListBean(data.getRow(), clazz);
            }
        }
        return null;
    }

    /**
     * 设置响应的业务状态和业务信息
     *
     * @param bussCode 业务响应代码
     * @param bussText 业务响应信息
     */
    public ResponseMessage setBussResponse(String bussCode, String bussText) {
        this.responseMessage.getMessage().getBody().getBussresponse().setCode(bussCode);
        this.responseMessage.getMessage().getBody().getBussresponse().setText(bussText);
        return this.responseMessage;
    }

    public void initBussResponse(){
        this.responseMessage.getMessage().getBody().setResponse(null);
    }

    /**
     * 设置响应的业务状态和业务信息
     *
     * @param ss [0] 业务响应代码
     * @param ss [1] 业务响应信息
     */
    public ResponseMessage setBussResponse(String[] ss) {
        this.responseMessage.getMessage().getBody().getBussresponse().setCode(ss[0]);
        this.responseMessage.getMessage().getBody().getBussresponse().setText(ss[1]);
        return this.responseMessage;
    }


    /**
     * 设置响应的业务状态和业务信息
     *
     * @param bussCode 业务响应代码
     * @param bussText 业务响应信息
     * @param language 用户当前选择的语言
     */
//    public ResponseMessage setBussResponse(String bussCode, String bussText, String language) {
//        this.responseMessage.getMessage().getBody().getBussresponse().setCode(bussCode);
//        this.responseMessage.getMessage().getBody().getBussresponse().setText(LangMapper.langValue(bussCode, language) + bussText);
//        return this.responseMessage;
//    }

    /**
     * 获取消息体中业务信息
     *
     * @return {@link Body#getBussresponse()}
     */
    public BussResponse getBussResponse() {
        return this.responseMessage.getMessage().getBody().getBussresponse();
    }

    /**
     * 设置查询响应
     *
     * @param pageno      查询的页面
     * @param pagesize    每页多少条
     * @param recordcount 总记录数
     * @param pagecount   总页面数
     */
    public void setQueryResponse(int pageno, int pagesize, int recordcount, int pagecount) {
        QueryResponse queryResponse = this.responseMessage.getMessage().getBody().getQueryresponse();
        if (queryResponse == null) {
            queryResponse = new QueryResponse();
            queryResponse.setPageno(pageno);
            queryResponse.setPagesize(pagesize);
            queryResponse.setRecordcount(recordcount);
            queryResponse.setPagecount(pagecount);

            this.responseMessage.getMessage().getBody().setQueryresponse(queryResponse);
        }
    }

    public void  initQueryResponse(){
        this.responseMessage.getMessage().getBody().setQueryresponse(null);
    }

    /**
     * 获取消息体中的响应信息
     *
     * @return {@link QueryResponse}
     */
    public QueryResponse getQueryResponse() {
        return this.responseMessage.getMessage().getBody().getQueryresponse();
    }
}
